#ifndef SKYID_COMMON_H
#define SKYID_COMMON_H

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstdarg>
#include <ctime>

#include "geom.h"

#ifdef SKYID_DEBUG
	//#define LOG_MSG(str,...) LogWrite(LOG_TSNL, "%s:%d "str, __FILE__, __LINE__, ##__VA_ARGS__)
	#define LOG_MSG(...) \
		Log::write(LOG_TSNL, __VA_ARGS__)

	#define LOG_ASSERT(what) \
		if (!(what)) \
		{ \
			LOG_MSG("Assert: " #what " (%s:%d)", __FILE__, __LINE__); \
			exit(1); \
		}

	#define DUMP_MEMORY(filename,mem,size) \
		do { \
			FILE *dumpFile = fopen(filename, "wb"); \
			if (!dumpFile) break; \
			LOG_MSG("Dumping memory 0x%p (size %d) to file '%s'", mem, size, filename); \
			fwrite(mem,  size, 1, dumpFile); \
			fclose(dumpFile); \
		} while(false)
#else
	#define LOG_MSG(...) 
	#define LOG_ASSERT(what) 
	#define DUMP_MEMORY(filename,mem,size) 
#endif

#define CALLBACK(p,...) if(p) (p)(__VA_ARGS__)

namespace SkyID
{

typedef unsigned char		uchar;
typedef unsigned short		uint16;
typedef unsigned long		uint32;
typedef unsigned long long	uint64;
typedef signed short		int16;
typedef signed long			int32;
typedef signed long long	int64;

//! Global variable to store log file name
static const char LOG_FILE_NAME[] = "sky-id.log";

//! Flags for additional information
enum LogFlags
{
    LOG_RAW  = 0, //! No additional information
    LOG_TS   = 1, //! Write timestamp at the beggining of the line
    LOG_NL   = 2, //! Write newline character at the end of the line
    LOG_TSNL = LOG_TS | LOG_NL //! Selfexplained
};

//! Check file extension
//! \param fileName Name of the file with extension
//! \param ext Checked extension
//! \return \c bool Return \c true if \c ext is extension of the \c fileName
bool CheckExt(const char *fileName, const char *ext);

namespace Log
{

//! Create log file. Must be called before any write!
//! \return \c bool Result of creating log file
bool create();

//! Write string to the log file
//! \param flags Request to write additional information
//! \param format Format string (in standart printf syntax)
//! \param ... Arguments for the format string
//! \return \c int Numbers of bytes writen
//! \sa logCreate
int write(LogFlags flags, const char *format, ...);

} // namespace Log

} // namespace SkyID

#endif // SKYID_COMMON_H
